package com.study.yangfei.mystudy.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.study.yangfei.mystudy.R;

public class JavaOptimizeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_optimize);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        SimpleSetBit mSetBit = new SimpleSetBit(128);
        for(int i=0;i<128;i++){
            boolean isOdd = i % 2 != 0;
            mSetBit.set(i,isOdd);
        }

        mSetBit.get(22);
        mSetBit.get(91);
    }

    public class SimpleSetBit{
        private static final int SIZEOF_INT = 32;
        private static final int OFFSET_MASK = SIZEOF_INT - 1;

        private int[] bits;

        public SimpleSetBit(int nBits){
            bits = new int[(nBits + SIZEOF_INT - 1)/SIZEOF_INT];
        }

        public void set(int index,boolean value){
            int i = index / SIZEOF_INT;
            int o = index & OFFSET_MASK;
            if(value){
                bits[i] |= 1<<o;
            }else{
                bits[o] &= ~(1<<o);
            }
        }

        public boolean get(int index){
            int i = index / SIZEOF_INT;
            int o = index & OFFSET_MASK;
            return 0 != (bits[i] & (1<<o));
        }
    }

}
